package com.omnisyr.textrebuilder.common;

import com.omnisyr.textrebuilder.util.BiSupplier;
import com.omnisyr.textrebuilder.util.structure.SimpleArrayList;

public class Absorber {

	private SimpleArrayList<Color> colors = new SimpleArrayList<Color>();

	public boolean absorb(Color color, BiSupplier<Color, Integer> loss) {

		if (this.colors.size() == 0) {

			this.colors.add(color);

			return false;

		}

		Color cloest = new Color(255, 255, 255);

		for (Color absorbor : this.colors) {

			if (loss.get(absorbor, color) < loss.get(cloest, color)) {

				cloest = absorbor;

			}

		}

		if (loss.get(cloest, color) <= 8) {

			return true;

		}

		this.colors.add(color);

		return false;

	}

	public Absorber sortByColor() {

		this.colors.sort(Color.hashComparator);

		return this;

	}

	public Palette toPalette() {

		Palette palette = new Palette();

		for (Color color : this.colors) {

			palette.append(color);

		}

		return palette;

	}

}
